package com.mp.approvalreq.dao.impl;

import com.mp.approvalreq.dao.OperateApprovalDao;
import com.plusmoney.util.SQLLogger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * Created by liuqi on 2018/7/3.
 */
public class OperateApprovalDaoImpl implements OperateApprovalDao {

	private static final Logger logger = LoggerFactory.getLogger(OperateApprovalDaoImpl.class);

	private Connection conn;

	public OperateApprovalDaoImpl(Connection conn) {
		this.conn = conn;
	}

	@Override
	public void switchAllSubTemplateTempCc(Integer ancestorId, Integer switchType) throws SQLException {

		String sql = " update approval.request_template set Fis_allow_add_temp_cc = ? " +
				" where Fancestor_id = ? ";

		try (PreparedStatement ps = conn.prepareStatement(sql)) {
			ps.setInt(1, switchType);
			ps.setInt(2, ancestorId);

			logger.info(SQLLogger.logSQL(sql, switchType, ancestorId));

			ps.executeUpdate();
		}
	}

	@Override
	public void updateOutPosition(String outPosition, Integer requestId) throws SQLException {
		String sql = " update request_flow set Fouter_position = ? where Frequest_id = ? limit 1";
		try (PreparedStatement ps = conn.prepareStatement(sql)) {
			ps.setString(1, outPosition);
			ps.setInt(2, requestId);
			logger.info(SQLLogger.logSQL(sql, outPosition, requestId));

			ps.executeUpdate();
		}
	}

	@Override
	public void updateContent(String content, Integer requestId, Integer num, Integer type) throws SQLException {
		String sql = " update request_content set Fcontent = ? " +
				" where Frequest_id = ? and Fcomponent_num = ? and Fcontent_type = ? limit 1";

		try (PreparedStatement ps = conn.prepareStatement(sql)) {
			ps.setString(1, content);
			ps.setInt(2, requestId);
			ps.setInt(3, num);
			ps.setInt(4, type);
			logger.info(SQLLogger.logSQL(sql, content, requestId, num, type));

			ps.executeUpdate();
		}
	}

}
	